<script>
import { mapMutations, mapActions } from "vuex";
import { inputInviteCode } from "@/api/user";
import { userShare, getConfig } from "@/api/app";
import Cache from "@/utils/cache";
import { strToParams, showModal, setTabbar } from "@/utils/tools";
import { wxMnpLogin } from "@/utils/login";
import {
	SHARE_INVITE_CODE,
	VERIFY_CODE
} from '@/config/cachekey';

export default {
  globalData: {
    navHeight: "",
  },
  onLaunch: async function (options) {
    // uni.hideTabBar({
    //   animation: false,
    // });
    // 获取配置
    this.getConfigFun();
	// 登录后才调用
	if(this.isLogin){
		this.getUser();
	}
    // 获取分享
    this.getShareInfo();
    //获取系统信息
    this.getSystemInfo();
  },
  onShow: function (options) {
	  // 存储从核销码进入
	  // this.setVerifyCode(options)
    //绑定邀请码
    this.bindCode(options);
  },
  onHide: function () {
    console.log("App Hide");
  },
  methods: {
    ...mapMutations(["SETCONFIG"]),
    ...mapActions(["getUser"]),
    getSystemInfo() {
      uni.getSystemInfo({
        success: (res) => {
          let { statusBarHeight, platform } = res;
          let navHeight;

          if (platform == "ios" || platform == "devtools") {
            navHeight = statusBarHeight + 44;
          } else {
            navHeight = statusBarHeight + 48;
          }
          this.globalData.navHeight = navHeight;
        },
        fail(err) {
          console.log(err);
        },
      });
    },
    async getShareInfo() {
      const { code, data } = await userShare();
      if (code == 1) {
        Cache.set("shareInfo", data);
      }
    },
    async getConfigFun() {
      try {
        const { code, data } = await getConfig();
        if (code == 1) {
          this.SETCONFIG(data);
          // setTabbar();
        }
      } catch (e) {
        // uni.showTabBar();
      }
    },
	setVerifyCode(options){
		if (!options.query) return;
		let verify_code =
		  options.query.s ||
		  strToParams(decodeURIComponent(options.query.scene)).s;
		console.log('verify_code', verify_code)
		if (verify_code) {
				Cache.set(VERIFY_CODE, verify_code); 
		}		  
				  
		// if (!options.scene) return;
		// let scene = options.scene;
		// console.log('scene', scene)
		// console.log('options', options)
		// let verifyObj = strToParams(decodeURIComponent(options.query.scene))
		// console.log('verifyObj:', verifyObj)
		// if (verifyObj.hasOwnProperty('s')) { // 是否是扫核销码进入的，从核销码进入的有"s"
		//   console.log(`对象包含键: s`);
		//   Cache.set(IS_VERIFY_CODE, true);
		//   uni.showModal({
		//   	title: "提示",
		//   	content: "是否确认核销该订单？",
		//   	confirmText: "确认",
		//   	cancelText: "取消",
		//   	success: (data) => {
		//   		if (data.confirm) {
		  			
		//   		}
		//   	}
		//   });
		// } else {
		//   console.log(`对象不包含键: s`);
		//   Cache.remove(IS_VERIFY_CODE);
		// }
	},
    bindCode(options) {
      if (!options.query) return;
      let invite_code =
        options.query.invite_code ||
        strToParams(decodeURIComponent(options.query.scene)).invite_code;
	  console.log('bindCode-invite_code', invite_code)
      if (invite_code) {
		Cache.set(SHARE_INVITE_CODE, invite_code); 
		  
        inputInviteCode({
          code: invite_code,
        }).then((res) => {
          if (res.code == -1) {
            Cache.set("INVITE_CODE", invite_code);
          }else{
			  uni.showToast({
			  	title: res.msg,
			  	icon: 'none',
			  	duration: 2000
			  });
		  }
        });
      }
    },
  },
};
</script>

<style lang="scss">
@import "styles/base.scss";
/*每个页面公共css */
@import "components/uview-ui/index.scss";
</style>
